import 'package:bot_toast/bot_toast.dart';
import 'package:easy_read_comic/controller/comic/search_list_controller.dart';
import 'package:easy_read_comic/view/comic/detail_view.dart';
import 'package:easy_read_comic/widgets/auto_image_widget.dart';
import 'package:easy_read_comic/widgets/search_widget.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SearchListView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: SearchListController(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).primaryColor,
            title: Text("搜索列表"),
          ),
          body: Column(
            children: [
              Container(
                padding: EdgeInsets.all(10),
                child: SearchWidget(
                  "请输入内容...",
                  value: controller.keyword,
                  onSubmitted: (value) {
                    if (value.isEmpty) {
                      BotToast.showText(
                        text: "请输入要搜索的漫画",
                      );
                      return;
                    }
                    controller.keyword = value;
                    controller.resetQueryComicList();
                  },
                ),
                color: Theme.of(context).primaryColor,
              ),
              Expanded(
                child: Container(
                  padding: EdgeInsets.only(
                    left: 10,
                    right: 10,
                    top: 10,
                  ),
                  child: EasyRefresh(
                    onRefresh: () {
                      controller.resetQueryComicList();
                    },
                    onLoad: () {
                      controller.queryComicList();
                    },
                    child: GridView.builder(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2, // 每行的列数
                        crossAxisSpacing: 5, // 列间距
                        mainAxisSpacing: 5, // 行间距
                        childAspectRatio: 0.8, // 子项宽高比
                      ),
                      itemBuilder: (context, index) {
                        return GestureDetector(
                          child: Container(
                            child: Stack(
                              children: [
                                Container(
                                  height: MediaQuery.of(context).size.height,
                                  child: ClipRRect(
                                    child: AutoImage.buildImage(
                                      controller.list[index].Thumb,
                                      fit: BoxFit.fitHeight,
                                    ),
                                    borderRadius: BorderRadius.circular(
                                      5,
                                    ),
                                  ),
                                ),
                                Positioned(
                                  bottom: 30,
                                  left: 10,
                                  child: Container(
                                    child: Text(
                                      controller.list[index].Title,
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 17,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ),
                                ),
                                Positioned(
                                  bottom: 10,
                                  left: 10,
                                  child: Container(
                                    child: Text(
                                      controller.list[index].Tags
                                          .join(" ")
                                          .toString(),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: TextStyle(
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          onTap: () {
                            Get.toNamed("/detail/${controller.list[index].Id}");
                          },
                        );
                      },
                      itemCount: controller.list.length,
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
